/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveCommands
{
	class GrooveExportMessages : CommandUtils.GrooveUtilBase
	{
		private bool m_ExcludeContent;
		private bool m_IncludeAttachments;
		private bool m_MarkMessagesOpened;
		private GrooveWebServicesV12Helpers.Message.Category m_Category;

		[STAThread]
		static void Main(string[] i_Arguments)
		{
			CommandUtils.UtilAppMain.RunApplication(new GrooveExportMessages(i_Arguments));
		}

		public GrooveExportMessages(string[] i_Arguments)
			: base (i_Arguments)
		{
			base.UsageMessage = "GrooveExportMessages [PARAMS...]";
			base.HelpMessage = 
				"GrooveExportMessages\r\n\t" + 
				"Exports Groove messages\r\n" +
				"Parameters:\r\n\t" +
				"[/Account=            The Web Services URI for the Groove account]\r\n\t" +
				"[/Identity=           The Web Services URI for the Groove identity]\r\n\t" +
				"[/ExcludeContent      Exclude message content]\r\n\t" +
				"[/IncludeAttachments  Include message attachments]\r\n\t" +
				"[/MarkMessagesOpened  Mark any unread messages as read]\r\n\t" +
				"[/Category=           \"Sent\", \"Opened\", \"Inbox\", \"New\" or \"All\" (default)\r\n\t]" +
				"[/RequestKey=         Token used to communicate Groove host]\r\n\t" +
				"[/Host=               The remote Groove host name to issue commands to]\r\n\t" +
				"[/Port=               The remote Groove host port to issue commands to]\r\n\t" +
				"[/Timeout=            Timeout (in ms) for Web Services method execution]\r\n\t" +
				"[/X=                  Configuration file to use for parameter input]\r\n\t" +
				"\r\n";
		}

		public override CheckResult CheckParameters()
		{
			CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
			if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded )
				return CheckResult;
		
			m_ExcludeContent = base.GetParameterValueFlag("ExcludeContent");

			m_IncludeAttachments = base.GetParameterValueFlag("IncludeAttachments");

			m_MarkMessagesOpened = base.GetParameterValueFlag("MarkMessagesOpened");

			string Category = base.GetParameterValue("Category");
			if (!System.String.IsNullOrEmpty(Category))
			{
				if (Category.ToLower() == "sent")
					m_Category = GrooveWebServicesV12Helpers.Message.Category.Sent;
				else if (Category.ToLower() == "opened")
					m_Category = GrooveWebServicesV12Helpers.Message.Category.Opened;
				else if (Category.ToLower() == "inbox")
					m_Category = GrooveWebServicesV12Helpers.Message.Category.Inbox;
				else if (Category.ToLower() == "new")
					m_Category = GrooveWebServicesV12Helpers.Message.Category.New;
				else if (Category.ToLower() == "all")
					m_Category = GrooveWebServicesV12Helpers.Message.Category.All;
				else
				{
					base.DisplayUsage("Invalid Category specified.");
					return CommandUtils.UtilBase.CheckResult.Failed;
				}
			}
			else
				m_Category = GrooveWebServicesV12Helpers.Message.Category.All;

			return base.CheckUnusedParameters();
		}

		public override CommandUtils.UtilBase.CheckResult Run()
		{
			GrooveWebServicesV12Helpers.Message[] Messages = null;
			
			try
			{
				Messages = base.Account.GetMessages
					(
					!m_ExcludeContent, 
					m_IncludeAttachments, 
					m_MarkMessagesOpened, 
					m_Category
					);
			}
			catch (System.Exception Exception)
			{
				base.StandardError.WriteLine("Unable to get messages: " + Exception.Message);
				throw Exception;
			}

			base.StandardOutput.WriteLine("<Messages Count='" + Messages.Length + "'>");

			foreach (GrooveWebServicesV12Helpers.Message Message in Messages)
			{
				string MessageOutput = 
					"<Message " +
					"Category='" + Message.Header.Category + "' " +
					"ID='" + CommandUtils.UtilStatic.XMLEncode(Message.Header.ID) + "' " +
					"URI='" + CommandUtils.UtilStatic.XMLEncode(Message.Header.URI) + "' " +
					"SenderName='" + Message.Header.Sender.Name + "' " +
					"SenderURI='" + CommandUtils.UtilStatic.XMLEncode(Message.Header.Sender.URI) + "' " +
					">";


				//  Recipients
				MessageOutput += "<Recipients>";
				foreach (GrooveWebServicesV12Helpers.MessageContact MessageContact in Message.Header.Recipients)
				{
					MessageOutput += "<Recipient " +
						"Name='" + CommandUtils.UtilStatic.XMLEncode(MessageContact.Name) + "' " +
						"URI='" + CommandUtils.UtilStatic.XMLEncode(MessageContact.URI) + "' " +
						"/>";
				}
				MessageOutput += "</Recipients>";

				if (!m_ExcludeContent)
				{
					MessageOutput += "<Body>" + Message.Content.Body + "</Body>";

					//  MessageAttachments
					if (m_IncludeAttachments)
					{
						if (Message.Header.HasAttachments)
						{
							System.Text.Encoding Encoding = new System.Text.UTF8Encoding();
							GrooveWebServicesV12Helpers.MessageAttachment[] MessageAttachments = Message.Content.Attachments;
							MessageOutput += "<Attachments Count='" + MessageAttachments.Length + "'>";

							foreach (GrooveWebServicesV12Helpers.MessageAttachment MessageAttachment in MessageAttachments)
							{
								MessageOutput += "<Attachment " +
									"Name='" + CommandUtils.UtilStatic.XMLEncode(MessageAttachment.DisplayName) + "' " +
									"Size='" + MessageAttachment.Size + "' " +
									">" +
									Encoding.GetString(MessageAttachment.Content) +
									"</Attachment>";
							}

							MessageOutput += "</Attachments>";
						}

						if (Message.Header.HasVoiceMemo)
						{
						}

					}
				}

				MessageOutput += "</Message>";

				base.StandardOutput.WriteLine(MessageOutput);
			}

			base.StandardOutput.WriteLine("</Messages>");

			return CommandUtils.UtilBase.CheckResult.Succeeded;
		}
	}

}
